var ProtoBuf = require('protobufjs');

/*
// 载入　wechat.proto 文件
var wechat_protobuf = ProtoBuf.loadProtoFile('./wehcat.proto'),
    wechat_pb = wechat_protobuf.build("MmBp"),
    authReq = wechat_pb.AuthRequest;

var auth_req = new auth_req({
    ProtoVersion: 1,
    AuthProto: 2
})

var buffer = auth_req.encode();

console.log(buffer.toString('hex'));
*/

var wxPbRoot = null;
/**
ProtoBuf.load("./wechat.proto", function(err, root) {
    if (err)
        throw err;

    wxPbRoot = root;

    var baseReq = root.lookupType("BaseRequest");
    var authReq = root.lookupType("AuthRequest");

    var base_test = {};

    var auth_test = {
        BaseRequest: base_test,
        ProtoVersion: 03, // 65540,
        AuthProto: 1,
        AuthMethod: 2
    };

    var errMsg = baseReq.verify(base_test);
    if (errMsg)
        throw Error(errMsg);

    errMsg = authReq.verify(auth_test);
    if (errMsg)
        throw Error(errMsg);

    var message = authReq.create(auth_test);

    var buffer = authReq.encode(message).finish();
    console.log("gump test1 " + buffer.toString('hex'));

    buffer = new Uint8Array([
        0x0A, 0x00, 0x18, 0x84, 0x80, 0x04, 0x20, 0x01,
        0x28, 0x02, 0x3A, 0x06, 0xC6, 0x05, 0x04, 0x00,
        0x00, 0x02, 0x62, 0x01, 0x00]);

    buffer = new Uint8Array([
        0x0A, 0x00, 0x18, 0x84, 0x80, 0x04, 0x20, 0x01,
        0x28, 0x02, 0x3A, 0x06, 0xC6, 0x00, 0x00, 0x00,
        0x00, 0x01, 0x62, 0x01, 0x00]);

    message = authReq.decode(buffer);
    console.log("gump test2 " + JSON.stringify(message));

    buffer = new Uint8Array([
        0x0a, 0x02, 0x08, 0x00, 0x12, 0x00
    ]);

    var authRes = root.lookupType("AuthResponse");
    message = authRes.decode(buffer);
    console.log("gump test3 " + JSON.stringify(message));
    
    buffer = new Uint8Array([
        0x0a, 0x00, 0x1a, 0x04, 0x11, 0x22, 0x33, 0x44
    ]);

    var initReq = root.lookupType("InitRequest");
    message = initReq.decode(buffer);
    console.log("gump test4 " + JSON.stringify(message));
    
    buffer = new Uint8Array([
        0x0a, 0x02, 0x08, 0x00, 0x10, 0xB4, 0x24, 0x18,
        0xF8, 0xAc, 0x01, 0x20, 0xD1, 0xBB, 0xCA, 0xBf,
        0x07
    ]);

    var initRes = root.lookupType("InitResponse");
    message = initRes.decode(buffer);
    console.log("gump test5 " + JSON.stringify(message));
});
*/
var jsonDescriptor = require("./wechat_pb.json");
wxPbRoot = ProtoBuf.Root.fromJSON(jsonDescriptor);

/*
var buffer = new Uint8Array([
    0x0A, 0x00, 0x18, 0x84, 0x80, 0x04, 0x20, 0x01,
    0x28, 0x02, 0x3A, 0x06, 0xC6, 0x05, 0x04, 0x00,
    0x00, 0x02, 0x62, 0x01, 0x00
]);
*/
/*
var authReq = wxPbRoot.lookupType("AuthRequest");
var message = authReq.decode(buffer);
console.log("gump test1 " + JSON.stringify(message));
*/
function wxPbDecode(buffer, pbname) {
    var field = wxPbRoot.lookupType(pbname);
    var message = field.decode(buffer);
    console.log("decode " + pbname + " " + JSON.stringify(message));
}

// wxPbDecode(buffer, "AuthRequest");

var EmCmdStr = {
    "ECI_none": "none",
    "ECI_req_auth": "AuthRequest",
    "ECI_req_sendData": "SendDataRequest",
    "ECI_req_init": "InitRequest",
    "ECI_resp_auth": "AuthResponse",
    "ECI_resp_sendData": "SendDataResponse",
    "ECI_resp_init": "InitResponse",
    "ECI_push_recvData": "RecvDataPush",
    "ECI_push_switchView": "SwitchViewPush",
    "ECI_push_switchBackgroud": "SwitchBackgroudPush",
    "ECI_err_decode": "error"
}


var srcData = new Uint8Array([
    0xFE, 0x01, 0x00, 0x1D, 0x27, 0x11, 0x00, 0x01,
    0x0A, 0x00, 0x18, 0x84, 0x80, 0x04, 0x20, 0x01,
    0x28, 0x02, 0x3A, 0x06, 0xC6, 0x05, 0x04, 0x00,
    0x00, 0x02, 0x62, 0x01, 0x00
]);

if (srcData[0] === 0xFE) {
    var length = (srcData[2] << 8) + srcData[3];
    var CmdId = (srcData[4] << 8) + srcData[5];
    var seq = (srcData[6] << 8) + srcData[7];
    var cmdEnum = wxPbRoot.getEnum("EmCmdId");
    var cmdStr = "";
    for (var key in cmdEnum) {
        if (cmdEnum[key] == CmdId) {
            // console.log("gump test:" + key + " " + JSON.stringify(EmCmdStr));
            cmdStr = EmCmdStr[key];
            break;
        }
    }
    // var cmdEnum = wxPbRoot.getEnum("EmCmdId", CmdId);
    console.log("\r\nReceived:length=" + length + " cmd=" + CmdId + " seq=" + seq + " " + cmdStr);
    var buf = srcData.slice(8);
    wxPbDecode(buf, cmdStr);
}
